fix(dsn): detect framework-prefixed DSN env vars in .env files and process.env#852
Merged
fix(dsn): detect framework-prefixed DSN env vars in .env files and process.env#852
Conversation
Contributor
|
…ocess.env (#820) `sentry init` was not detecting DSNs supplied via framework-specific environment variables like `NEXT_PUBLIC_SENTRY_DSN`, `REACT_APP_SENTRY_DSN`, `VITE_SENTRY_DSN`, etc. This caused init to create a new Sentry project instead of reusing the existing one when the DSN was provided through a framework-prefixed env var. The .env file scanner (env-file.ts) only matched the exact pattern `SENTRY_DSN=...`. The runtime env detection (env.ts) only checked `process.env.SENTRY_DSN`. Fix: - Expanded the .env file regex from `^SENTRY_DSN` to `^(?:\w+_)?SENTRY_DSN` to match framework-prefixed variants (NEXT_PUBLIC_, REACT_APP_, VITE_, etc.) - Added explicit checks for the 5 most common framework-prefixed env vars in the runtime env detection, checked after the canonical SENTRY_DSN Co-authored-by: Miguel Betegón <miguelbetegongarcia@gmail.com>
f9e2abb to
788ce91
Compare
Contributor
Codecov Results 📊✅ 6554 passed | Total: 6554 | Pass Rate: 100% | Execution Time: 0ms 📊 Comparison with Base Branch
All tests are passing successfully. ✅ Patch coverage is 100.00%. Project has 13294 uncovered lines. Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 76.02% 76.10% +0.08%
==========================================
Files 296 296 —
Lines 55640 55618 -22
Branches 0 0 —
==========================================
+ Hits 42298 42324 +26
- Misses 13342 13294 -48
- Partials 0 0 —Generated by Codecov Action |
Address Bugbot review: - Invalid DSN in SENTRY_DSN no longer causes premature return; the loop continues to check framework-prefixed vars. - The actual env var name (e.g. NEXT_PUBLIC_SENTRY_DSN) is passed as sourcePath so getDsnSourceDescription reports the correct variable.
Contributor
Author
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 30b4cc3. Configure here.
…tests - Export FRAMEWORK_ENV_PREFIXES from env.ts and build the env-file.ts regex from them, so both detection paths match exactly the same set of framework-prefixed variable names (no open \w+ regex). - Update sourcePath JSDoc in types.ts to document dual semantics (env var name for 'env' source, file path for file-based sources). - Update DsnSource doc to mention framework-prefixed env vars. - Add test/lib/dsn/env.test.ts covering: all framework vars, canonical priority, invalid-DSN skip-through, sourcePath propagation. - Add framework-prefix tests to env-file.test.ts (match + reject). - Add getDsnSourceDescription test for env source with sourcePath.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
DSN detection only matched
SENTRY_DSN=...in .env files andprocess.env.SENTRY_DSNat runtime. Framework-prefixed variants likeNEXT_PUBLIC_SENTRY_DSN,REACT_APP_SENTRY_DSN,VITE_SENTRY_DSNwere not detected, causingsentry initto create a new project instead of reusing the existing one.Trigger: Having a DSN via
NEXT_PUBLIC_SENTRY_DSNin.env.localand runningsentry init.Changes
env.ts: Added aFRAMEWORK_ENV_PREFIXESallowlist and a loop that checksSENTRY_DSNfirst, then framework-prefixed variants. Invalid DSNs are skipped (falls through to next candidate). The matched var name is passed assourcePathso source descriptions are accurate.env-file.ts: Replaced the open^(?:\w+_)?SENTRY_DSNregex with one built from the sharedFRAMEWORK_ENV_PREFIXESallowlist, so both detection paths match exactly the same set of variable names.detector.ts:getDsnSourceDescriptionnow usesdsn.sourcePathfor the"env"case (falls back toSENTRY_DSNwhen unset).types.ts: UpdatedsourcePathJSDoc to document dual semantics (env var name for"env"source, file path for file-based sources).test/lib/dsn/env.test.ts(12 tests covering all framework vars, canonical priority, invalid-DSN skip-through, empty values). Added framework-prefix match/reject tests toenv-file.test.ts. AddedgetDsnSourceDescriptiontest for env source withsourcePath.Fixes #820